﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32.SafeHandles;

namespace O1.IO.Storage
{
    internal class NtfsTxNativeMethods
    {
        const uint GENERIC_READ = 0x80000000;

        const uint GENERIC_WRITE = 0x40000000;
        
        const uint CREATE_NEW = 1;
        
        const uint CREATE_ALWAYS = 2;
        
        const uint OPEN_EXISTING = 3;

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern SafeFileHandle CreateFile(
           string lpFileName,
           uint dwDesiredAccess,
           uint dwShareMode,
           IntPtr lpSecurityAttributes,
           uint dwCreationDisposition,
           uint dwFlagsAndAttributes,
           IntPtr hTemplateFile);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern SafeFileHandle CreateFileTransacted(
           string lpFileName,
           uint dwDesiredAccess,
           uint dwShareMode,
           IntPtr lpSecurityAttributes,
           uint dwCreationDisposition,
           uint dwFlagsAndAttributes,
           IntPtr hTemplateFile,
           IntPtr hTransaction,
           IntPtr pusMiniVersion,
           IntPtr pExtendedParameter);

        [DllImport("ktmw32.dll", SetLastError = true)]
        static extern IntPtr CreateTransaction(
           IntPtr lpTransactionAttributes,
           IntPtr uow,
           uint createOptions,
           uint isolationLevel,
           uint isolationFlags,
           uint timeout,
           string description);

        [DllImport("ktmw32.dll", SetLastError = true)]
        static extern bool CommitTransaction(
           IntPtr transaction);

        [DllImport("ktmw32.dll", SetLastError = true)]
        static extern bool RollbackTransaction(
           IntPtr transaction);

        [DllImport("Kernel32.dll")]
        static extern bool CloseHandle(IntPtr handle);
    }
}
